package nxu.it.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;

/**
 * @author yangliu
 * @project jfinal-app-1
 * @sid 12020243892
 * @date 2022/10/31 - 9:34
 */
public class requireLogin implements Interceptor {

    @Override
    public void intercept(Invocation invocation) {
//        参数为被拦截的方法
        Controller controller = invocation.getController();
        String role = controller.getSessionAttr("role");
        if (role==null){
            System.out.println("未登录，拦截到登录界面");
            controller.redirect("/login");
        }else {

            if (!role.equals("admin")&&controller.getRequest().getRequestURI().contains("admin")){
                System.out.println("非管理，到main");
                controller.redirect("/main");
            }
        }
    }
}
